﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BoutiquePOS.DAL.DAO;
using BoutiquePOS.DAL.Gateway;
using BoutiquePOS.Manager;

namespace BoutiquePOS.UI.UserUI
{
    public partial class SalesReturnEntry : System.Web.UI.Page
    {
        SalesReturnDtlsManager aSalesReturnDtlsManager = new SalesReturnDtlsManager();
        List<SalesReturnDtls> salesReturnDtlses = new List<SalesReturnDtls>();

        private int salesId = 0;
        SalesReturn salesReturn = new SalesReturn();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                try
                {
                    string dateAsString = DateTime.Now.ToString("yyyy-MM-dd");
                    returnDateTextBox.Value = dateAsString;
                    Session["totalNetAmount"] = 0;
                    Session["ProductList"] = salesReturnDtlses;
                    Session["SalesReturn"] = 0;
                }
                catch (Exception)
                {
                    throw;
                }
            } 
        }

        protected void salesReturnGridView_RowEditing(object sender, GridViewEditEventArgs e)
        {
            salesReturnGridView.EditIndex = e.NewEditIndex;
        }
        protected void salesReturnGridView_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            salesReturnGridView.EditIndex = -1;
        }

        protected void SaveSalesReturn(object sender, EventArgs e)
        {
            try
            {
                messageResult.InnerText = "";
                if (Convert.ToInt32(Session["SalesReturn"]) > 0)
                {
                    SalesReturn salesReturn = new SalesReturn();
                    salesReturn.SalesId = Convert.ToInt32(Session["salesId"]);
                    salesReturn.SalesReturnDate = returnDateTextBox.Value;
                    salesReturn.TotalNetAmount = Convert.ToDecimal(Session["totalNetAmount"]);
                    aSalesReturnDtlsManager.SaveSalesReturn(salesReturn);

                    int id = aSalesReturnDtlsManager.SalesReturnId(salesReturn.SalesId);
                    salesReturnDtlses = (List<SalesReturnDtls>)Session["ProductList"];
                    aSalesReturnDtlsManager.SaveSalesReturnDtls(id, salesReturnDtlses);
                    Session["SalesReturn"] = 0;
                }
                else
                {
                    messageResult.InnerText = "You should update the sales quantity first";
                }

            }
            catch (Exception)
            {
                throw;
            }
        }

        protected void salesReturnGridView_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            try
            {
                SalesReturnDtls aSalesReturnDtls = new SalesReturnDtls();
                SalesReturnDtlsManager aSalesReturnDtlsManager = new SalesReturnDtlsManager();
                Session["SalesReturn"] = 1;

                int selectedIndex = e.RowIndex;
                int salesId1 = Convert.ToInt32(((Label)salesReturnGridView.Rows[selectedIndex].FindControl("lblSalesId")).Text);
                int returnQty = Convert.ToInt32(((TextBox)salesReturnGridView.Rows[selectedIndex].FindControl("txtReturnQty")).Text);
                int returnFreeQty = Convert.ToInt32(((TextBox)salesReturnGridView.Rows[selectedIndex].FindControl("txtFreeReturnQty")).Text);
                int productId = Convert.ToInt32(((Label)salesReturnGridView.Rows[selectedIndex].FindControl("lblProductId")).Text);
                decimal mrp = Convert.ToDecimal(((Label)salesReturnGridView.Rows[selectedIndex].FindControl("lblMRP")).Text);
                decimal discountPer = Convert.ToInt32(((Label)salesReturnGridView.Rows[selectedIndex].FindControl("lblDiscountPer")).Text);

                decimal discountAmount = (discountPer * mrp) / 100 * returnQty;
                decimal totalReturnAmount = Convert.ToDecimal(Session["totalNetAmount"]);
                totalReturnAmount = totalReturnAmount + (returnQty * mrp) - discountAmount;
                Session["totalNetAmount"] = totalReturnAmount;

                salesReturnDtlses = (List<SalesReturnDtls>)Session["ProductList"];
                aSalesReturnDtls.ProductId = productId;
                aSalesReturnDtls.ReturnQty = returnQty;
                aSalesReturnDtls.ReturnFreeQty = returnFreeQty;
                aSalesReturnDtls.SalesId = salesId1;
                salesReturnDtlses.Add(aSalesReturnDtls);
                Session["ProductList"] = salesReturnDtlses;

                Session["salesId"] = salesId1;
                salesReturnGridView.EditIndex = -1;
                LoadGridView();
            }
            catch (Exception)
            {
                throw;
            }
        }

        private void LoadGridView()
        {
            try
            {
                SalesGateway salesGateway = new SalesGateway();
                int id = salesGateway.ReturnSalesId(salesNoTextBox.Value);
                SalesReturnDtlsGateway salesReturnDtlsGateway = new SalesReturnDtlsGateway();
                List<SalesDtls> salesDtlses = salesReturnDtlsGateway.GetAllSalesProduct(id);
                salesReturnGridView.DataSource = salesDtlses;
                salesReturnGridView.DataBind();
            }
            catch (Exception)
            {
                throw;
            }
        }

        public void SearchButton(object sender, EventArgs e)
        {
            LoadGridView();
        }
        public void CloseButton(object sender, EventArgs e)
        {
            Response.Redirect("Home.aspx");
        }
        public void ClearButton(object sender, EventArgs e)
        {
            Clear();
        }
        private void Clear()
        {
            salesNoTextBox.Value = "";
        }

    }
}